package com.suke.czx.modules.wx.controller;

import java.util.Date;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.suke.czx.common.utils.JsonResult;
import com.suke.czx.common.utils.RedisUtils;
import com.suke.czx.common.utils.Tools;
import com.suke.czx.modules.wx.cons.Constant;
import com.suke.czx.modules.wxadmin.entity.DkUserEntity;
import com.suke.czx.modules.wxadmin.entity.DkUserPhoneEntity;
import com.suke.czx.modules.wxadmin.service.DkUserPhoneService;
import com.suke.czx.modules.wxadmin.service.DkUserService;

import me.chanjar.weixin.mp.bean.result.WxMpUser;

@RestController
@RequestMapping("/wx/userinfo")
public class UserInfoController extends BaseController {

	DefaultProfile profile = DefaultProfile.getProfile("default", "DE929rKOa8bzsYY5", "zat5D8YbCPnOzYnTXWipjRGD2n5W4n");
	IAcsClient client = new DefaultAcsClient(profile);
	private org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private DkUserService dkUserService;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired
	private DkUserPhoneService dkUserPhoneService;

	@RequestMapping("/getcode")
	public String getcode(String phone) {

		WxMpUser wxMpUser = (WxMpUser) getSession().getAttribute(Constant.WX_USER);
		if (null == wxMpUser) {
			return new JsonResult(24, "用户未登录，请先登录！").toJson();
		}

		DkUserEntity dkUser = dkUserService.queryObject(wxMpUser.getOpenId(),true);
		if (dkUser.getState() == 0) {
			return new JsonResult(23, "用户未订购，请跳转购买链接！").toJson();
		}

		if (null == phone || !Tools.checkMobileNumber(phone)) {
			return new JsonResult(-1, "请输入正确的手机号！").toJson();
		}
		//判断用户是否绑定过
		DkUserPhoneEntity userPhone = dkUserPhoneService.queryObject(dkUser.getOpenid());
		if(userPhone!=null){
			return new JsonResult(-1, "该用户已经绑定过手机了！").toJson();
		}
		userPhone = dkUserPhoneService.queryObjectByPhone(phone);
		if(userPhone!=null){
			return new JsonResult(-1, "该手机号已经绑定过了！").toJson();
		}
		Integer code = Tools.getRandomNum();

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", "诗词精读210天");
		request.putQueryParameter("TemplateCode", "SMS_167290002");
		request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			//设置缓存
			redisUtils.setObject(dkUser.getOpenid()+phone, String.valueOf(code), 305);
			logger.info("==========> 绑定手机：" + "openid：" + dkUser.getOpenid() + " code：" + code);
			return new JsonResult(200,"ok").toJson();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new JsonResult(-1,"短信发送收到限制！").toJson();

	}
	
	@RequestMapping("/verify")
	public String verify() {
		WxMpUser wxMpUser = (WxMpUser) getSession().getAttribute(Constant.WX_USER);
		if (null == wxMpUser) {
			return new JsonResult(24, "用户未登录，请先登录！").toJson();
		}
		//判断用户是否绑定过
		DkUserPhoneEntity userPhone = dkUserPhoneService.queryObject(wxMpUser.getOpenId());
		if(userPhone!=null){
			return new JsonResult(200, "ok",userPhone).toJson();
		}
		return new JsonResult(200, "用户未绑定！").toJson();
	}
	
	
	@RequestMapping("/bind")
	public String bind(String phone,String code) {
		WxMpUser wxMpUser = (WxMpUser) getSession().getAttribute(Constant.WX_USER);
		if (null == wxMpUser) {
			return new JsonResult(24, "用户未登录，请先登录！").toJson();
		}

		DkUserEntity dkUser = dkUserService.queryObject(wxMpUser.getOpenId(),true);
		if (dkUser.getState() == 0) {
			return new JsonResult(23, "用户未订购，请跳转购买链接！").toJson();
		}

		if (null == phone || !Tools.checkMobileNumber(phone)) {
			return new JsonResult(-1, "请输入正确的手机号！").toJson();
		}
		
		String r_code = redisUtils.getObject(dkUser.getOpenid()+phone);
		if(code!=null&&r_code.equals(code)){
			DkUserPhoneEntity userPhone = new DkUserPhoneEntity();
			userPhone.setUserId(dkUser.getOpenid());
			userPhone.setPhone(phone);
			userPhone.setCreateTime(new Date());
			dkUserPhoneService.save(userPhone);
			return new JsonResult(200,"绑定成功").toJson();
		}
		return new JsonResult(-1,"绑定失败").toJson();
	}
	

}
